package com.shycloud.mido.organ.test.basic;

/**
 * @author Xiaoxing Yu
 * @date 2020/8/24
 **/
public class DeadLockTest2 {

	public static final String obj1 = "obj1";
	public static final String obj2 = "obj2";

	public static void main(String[] ars) {
		new DeadLockA().start();
		new DeadLockB().start();
	}

}

class DeadLockA extends Thread {

	@Override
	public void run() {
		try{
			System.out.println("LockA running");
			while(true){
				synchronized(DeadLockTest2.obj1){
					System.out.println("LockA locked obj1");
					//获取obj1后先等一会儿，让LockB有足够的时间锁住obj2
					Thread.sleep(100);
					System.out.println("LockA trying to lock obj2...");
					synchronized(DeadLockTest2.obj2){
						System.out.println("LockA locked obj2");
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}

}

class DeadLockB extends Thread {
	@Override
	public void run() {
		try{
			System.out.println("LockB running");
			while(true){
				synchronized(DeadLockTest2.obj2){
					System.out.println("LockB locked obj2");
					System.out.println("LockB trying to lock obj1...");
					synchronized(DeadLockTest2.obj1){
						System.out.println("LockB locked obj1");
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
